layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index', //主入口模块
}).use(['index', 'echarts', 'carousel', 'senior'], function(){
    var $ = layui.$,
        echarts = layui.echarts;

    $(document).ready(function(){
        if($('#registerStatics').length > 0){
            //初始化统计图表
            getRegisterStatics(1);

            $(document).on('click', '#year-reg', function () {
                getRegisterStatics(2);
            });
            $(document).on('click', '#month-reg', function () {
                getRegisterStatics(1);
            });
        }

        if($('#rechargeWithdrawStatics').length > 0){
            //初始化统计图表
            getRechargeWithdrawStatics(1);

            $(document).on('click', '#year-recW', function () {
                getRechargeWithdrawStatics(2);
            });
            $(document).on('click', '#month-recW', function () {
                getRechargeWithdrawStatics(1);
            });
        }
    })

    /** =====================数据统计 start======================= */

    /**
     * 渲染统计图表
     * @param type
     */
    function getRegisterStatics(type){
        var data = {'type': type};
        var totalUserNum = new Array();
        var womanNum = new Array();
        var manNum = new Array();
        var inviteNum = new Array();
        var notInviteNum = new Array();
        var newRechargeNum = new Array();
        // var totalRechargeNum = new Array();
        var createTime = new Array();
        post('home/registerStatics', data, false,function (data) {
            var registerUserTotal = data.registerUserTotal;
            $('#registerUser').html("总注册人数：" + registerUserTotal);

            var registerStaticsList = data.registerStaticsList;
            for(var i in registerStaticsList){
                var staticsMap = registerStaticsList[i];
                for(var j in staticsMap){
                    if(j == 'totalUserNum'){
                        totalUserNum.push(staticsMap[j]);
                    }else if(j == 'womanNum'){
                        womanNum.push(staticsMap[j])
                    }else if(j == 'manNum'){
                        manNum.push(staticsMap[j])
                    }else if(j == 'inviteNum'){
                        inviteNum.push(staticsMap[j])
                    }else if(j == 'notInviteNum'){
                        notInviteNum.push(staticsMap[j])
                    }else if(j == 'newRechargeNum'){
                        newRechargeNum.push(staticsMap[j])
                    }/*else if(j == 'totalRechargeNum'){
                        totalRechargeNum.push(staticsMap[j])
                    }*/else if(j == 'createTime'){
                        createTime.push(staticsMap[j]);
                    }
                }
            }

            var echnormcol = [], normcol = [{
                    tooltip : {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'shadow'
                        }
                    },
                    legend: {data:['总注册人数', '男用户量', '女用户量', '邀请注册量', '非邀请注册量', '新充值人数'/*, '总充值人数'*/]},
                    calculable : true,
                    xAxis : [{
                        type : 'category',
                        data : createTime
                    }],
                    yAxis : [{type:"value"}],
                    series : [
                        {
                            name:'总注册人数',
                            type:'bar',
                            barWidth: 10,
                            data: totalUserNum
                        },
                        {
                            name:'男用户量',
                            type:'bar',
                            stack: '性别',
                            itemStyle:{
                                normal:{
                                    color:'#D87A80'
                                }
                            },
                            barWidth: 5,
                            data: manNum
                        },
                        {
                            name:'女用户量',
                            type:'bar',
                            stack: '性别',
                            itemStyle:{
                                normal:{
                                    color:'#FFB980'
                                }
                            },
                            data: womanNum
                        },
                        {
                            name:'邀请注册量',
                            type:'bar',
                            stack: '邀请',
                            itemStyle:{
                                normal:{
                                    color:'#97B552'
                                }
                            },
                            barWidth: 5,
                            data: inviteNum
                        },
                        {
                            name:'非邀请注册量',
                            type:'bar',
                            stack: '邀请',
                            itemStyle:{
                                normal:{
                                    color:'#E5CF0D'
                                }
                            },
                            data: notInviteNum
                        },
                        /*{
                            name:'总充值人数',
                            type:'bar',
                            itemStyle:{
                                normal:{
                                    color:'#CAE1FF'
                                }
                            },
                            barWidth: 5,
                            data: totalRechargeNum
                        },*/
                        {
                            name:'新充值人数',
                            type:'bar',
                            itemStyle:{
                                normal:{
                                    color:'#8d98b3'
                                }
                            },
                            barWidth: 5,
                            data: newRechargeNum
                        }
                    ]
                }],
                elemNormcol = $('#LAY-index-normcol-reg').children('div'),
                renderNormcol = function(index){
                    echnormcol[index] = echarts.init(elemNormcol[index], layui.echartsTheme);
                    echnormcol[index].setOption(normcol[index]);
                    window.onresize = echnormcol[index].resize;
                };
            if(!elemNormcol[0]) return;
            renderNormcol(0);
        });
    }

    function getRechargeWithdrawStatics(type){
        var data = {'type': type};
        var rechargeNum = new Array();
        var withdrawNum = new Array();
        var vipRechargeNum = new Array();
        var payRechargeNum = new Array();
        var payPatternRechargeNum = new Array();
        var payVipNum = new Array();
        var payPatternVipNum = new Array();
        var newUserRechargeNum = new Array();
        var oldUserRechargeNum = new Array();
        var createTime = new Array();
        post('home/rechargeWithdrawStatics', data, false,function (data) {
            var rechargeAndWithdrawTotal = data.rechargeAndWithdrawTotal;
            $('#rechargeAndWithdraw').html("总充值金额：" + rechargeAndWithdrawTotal.rechargeTotal
                + "<br>总提现金额：" + rechargeAndWithdrawTotal.withdrawTotal);

            var rechargeWithdrawStaticsList = data.rechargeWithdrawStaticsList;
            for(var i in rechargeWithdrawStaticsList){
                var staticsMap = rechargeWithdrawStaticsList[i];
                for(var j in staticsMap){
                    if(j == 'rechargeNum'){
                        rechargeNum.push(staticsMap[j]);
                    }else if(j == 'withdrawNum'){
                        withdrawNum.push(staticsMap[j])
                    }else if(j == 'vipRechargeNum'){
                        vipRechargeNum.push(staticsMap[j])
                    }else if(j == 'payRechargeNum'){
                        payRechargeNum.push(staticsMap[j])
                    }else if(j == 'payPatternRechargeNum'){
                        payPatternRechargeNum.push(staticsMap[j])
                    }else if(j == 'payVipNum'){
                        payVipNum.push(staticsMap[j])
                    }else if(j == 'payPatternVipNum'){
                        payPatternVipNum.push(staticsMap[j])
                    }else if(j == 'newUserRechargeNum'){
                        newUserRechargeNum.push(staticsMap[j])
                    }else if(j == 'oldUserRechargeNum'){
                        oldUserRechargeNum.push(staticsMap[j])
                    }else if(j == 'createTime'){
                        createTime.push(staticsMap[j]);
                    }
                }
            }

            var echnormcol = [], normcol = [{
                    tooltip : {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'shadow'
                        }
                    },
                    legend: {data:['P币充值', '会员充值', '提现', 'P币原生支付', 'P币第三方支付', '会员原生支付',
                            '会员第三方支付', '新用户充值', '老用户充值']},
                    calculable : true,
                    xAxis : [{
                        type : 'category',
                        data : createTime
                    }],
                    yAxis : [{type:"value"}],
                    series : [
                        {
                            name:'P币充值',
                            type:'bar',
                            stack: '充值',
                            barWidth: 10,
                            data: rechargeNum
                        },
                        {
                            name:'P币原生支付',
                            type:'bar',
                            stack: '支付模式',
                            itemStyle:{
                                normal:{
                                    color:'#D87A80'
                                }
                            },
                            barWidth: 5,
                            data: payRechargeNum
                        },
                        {
                            name:'P币第三方支付',
                            type:'bar',
                            stack: '支付模式',
                            itemStyle:{
                                normal:{
                                    color:'#FFB980'
                                    // color:'#E5CF0D'
                                }
                            },
                            data: payPatternRechargeNum
                        },
                        {
                            name:'会员充值',
                            type:'bar',
                            stack: '充值',
                            data: vipRechargeNum
                        },
                        {
                            name:'会员原生支付',
                            type:'bar',
                            stack: '支付模式',
                            itemStyle:{
                                normal:{
                                    color:'#97B552'
                                }
                            },
                            data: payVipNum
                        },
                        {
                            name:'会员第三方支付',
                            type:'bar',
                            stack: '支付模式',
                            itemStyle:{
                                normal:{
                                    color:'#E5CF0D'
                                }
                            },
                            data: payPatternVipNum
                        },
                        {
                            name:'新用户充值',
                            type:'bar',
                            stack: '用户充值',
                            itemStyle:{
                                normal:{
                                    color:'#8d98b3'
                                }
                            },
                            barWidth: 5,
                            data: newUserRechargeNum
                        },
                        {
                            name:'老用户充值',
                            type:'bar',
                            stack: '用户充值',
                            itemStyle:{
                                normal:{
                                    color:'#CAE1FF'
                                }
                            },
                            data: oldUserRechargeNum
                        },
                        {
                            name:'提现',
                            type:'bar',
                            itemStyle:{
                                normal:{
                                    color:'#95706D'
                                }
                            },
                            barWidth: 10,
                            data: withdrawNum
                        }
                    ]
                }],
                elemNormcol = $('#LAY-index-normcol-recW').children('div'),
                renderNormcol = function(index){
                    echnormcol[index] = echarts.init(elemNormcol[index], layui.echartsTheme);
                    echnormcol[index].setOption(normcol[index]);
                    window.onresize = echnormcol[index].resize;
                };
            if(!elemNormcol[0]) return;
            renderNormcol(0);
        });
    }
    /** =====================数据统计 end===================== */

});